/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plagiadetector.utility;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dell
 */
public class DummyDump {
    
     private static FileWriter fw;
     private static BufferedReader br;
    /**
     * Generate performance dump from an evaluation dump.
     * Output file contains performance dump for a document in each line. The sequence is [Precision] [Recall] [Granularity].
     * Output file name is inputaddr+"_perfdump.txt".
     * @param inputaddr Input file address.
     */
    public static void generatePerformanceDump(String inputaddr) throws IOException{
        String readbuff = "";
        String writebuff = "";
        br = new BufferedReader(new FileReader(inputaddr));
        fw = new FileWriter(inputaddr.replaceAll(".txt", "")+"_perfdump.txt");
        
        while((readbuff = br.readLine()) != null){
            if(readbuff.contains("Precision : ")){
                writebuff = readbuff.replaceAll("Precision : ", "");
            }else if(readbuff.contains("Recall : ")){
                writebuff += " "+readbuff.replaceAll("Recall : ", "");
            }else if(readbuff.contains("Granularity : ")){
                writebuff += " "+readbuff.replaceAll("Granularity : ", "");
                fw.write(writebuff+"\n");
                fw.flush();
            }
        }
        
        br.close();
        fw.close();
        
    }
    
    public static void getTotalPerformance(String perfdumaddr) throws IOException{
        String readbuff = "";
        String[] perfbuff;
        int totalsize = 0;
        int validsize = 0;
        float precbuff = 0f;
        float recbuff = 0f;
        float grabuff = 0f;
        
        br = new BufferedReader(new FileReader(perfdumaddr));
        
        while((readbuff = br.readLine())!= null){
            totalsize++;
            perfbuff = readbuff.split(" ");
            if(Float.parseFloat(perfbuff[0]) > 0f){ //if its a valid data (not zero)
                validsize++;
                precbuff += Float.parseFloat(perfbuff[0]);
                recbuff += Float.parseFloat(perfbuff[1]);
                grabuff += Float.parseFloat(perfbuff[2]);
            }
        }
        
        precbuff /= validsize;
        recbuff /= validsize;
        grabuff /= validsize;
        float plagdet = (float) (((2*precbuff*recbuff)/(precbuff+recbuff))/Math.log(1+grabuff));
        System.out.println("Total line : "+totalsize);
        System.out.println("Valid line : "+validsize);
        System.out.println("Global Precision : "+precbuff);
        System.out.println("Global Recall : "+recbuff);
        System.out.println("Global Granularity : "+grabuff);
        System.out.println("Global Plagdet : "+plagdet);

        br.close();

    }
    
    public static void main(String[] args){
        try {
            DummyDump.generatePerformanceDump("D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\PlagiaDetector\\PlagiaDetector\\dummydump.txt");
            DummyDump.getTotalPerformance("D:\\Kuliah\\Semester 8\\IF4099 Tugas Akhir  II\\Implementasi\\PlagiaDetector\\PlagiaDetector\\dummydump_perfdump.txt");
        } catch (IOException ex) {
            Logger.getLogger(DummyDump.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
